/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.sigawe.gateway.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import net.sigawe.gateway.dao.interfc.IOutboxDAO;
import net.sigawe.gateway.model.Outbox;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ServerGawe
 */
public class OutboxDAO implements IOutboxDAO {

    private Connection connection = null;

    public OutboxDAO(Connection connection) {
        this.connection = connection;
    }
    
    public synchronized void delete(int idOutbox) throws SQLException {
        String sql = "DELETE FROM outbox WHERE id=?";
        PreparedStatement statement = this.connection.prepareStatement(sql);
        statement.setInt(1, idOutbox);
        statement.executeUpdate();
        statement.close();
    }

    public List<Outbox> getAll() throws SQLException {
        String sql = "SELECT id,receiver,text FROM outbox";
        ResultSet rs = null;
        PreparedStatement statement = this.connection.prepareStatement(sql);
        rs = statement.executeQuery();
        Outbox outbox = new Outbox();
        List<Outbox> list = new ArrayList<Outbox>();
        while(rs.next())
        {
            outbox.setIdOutbox(rs.getInt("id"));
            outbox.setReceiver(rs.getString("Receiver"));
            outbox.setText(rs.getString("text"));
            list.add(outbox);
        }
        rs.close();
        statement.close();
        return list;
    }
    @Override
    public void insert(Outbox outbox)throws SQLException
    {
        String sql = "INSERT INTO outbox VALUES(null,?,?)";
        PreparedStatement ps = this.connection.prepareStatement(sql);
        ps.setString(1, outbox.getReceiver());
        ps.setString(2, outbox.getText());
        ps.executeUpdate();
        System.out.println("Insert Outbox OKE");
        ps.close();
    }
}
